Imports System.Windows.Forms
Imports System.Drawing

Public Class Plug
    Inherits UserControl
    Private P1 As Point
    Private M_Rad As Integer = 5
    Private M_BodyWidth As Integer = 12
    Private M_SocketType As Socket_Type = Socket_Type.None
    Private M_Exit_to As Exit_Side = Exit_Side.To_Right
    Private M_Center_Point As Point
    Private M_Length As Integer = 40
    Private components As System.ComponentModel.IContainer
    Friend WithEvents ContextMenuStrip1 As System.Windows.Forms.ContextMenuStrip
    Friend WithEvents DisconnectToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Private M_Color As Color
    Private M_Color_B As Color

    Friend WithEvents Button1 As System.Windows.Forms.Button
    Private M_Sock As Socket
    Public Event Disconnect()
    Private M_LinePoint As Point
    Private m_AutoBodyColor As Boolean = True
    Private m_BodyColor As Color = Color.Black
    Public Event UpdateWire(ByVal NewPoint As Point)
    Private m_oldPoint As Point
    Private SocketTypesColor As New SocketTypes

    Enum Socket_Type
        None = 0
        Video = 1
        Trigger = 2
        Image = 3
        CameraControl = 4
        Control = 5
        Universal = 6
        HardwarePort = 7
        Selected = 10
    End Enum
    Enum Exit_Side
        None = 0
        To_Top = 1
        To_Bottom = 2
        To_Left = 3
        To_Right = 4
    End Enum
    Public Property ConnectedSocket() As Socket
        Get
            Return M_Sock
        End Get
        Set(ByVal value As Socket)
            M_Sock = value
        End Set
    End Property
    Public Property BodyWidth() As Integer
        Get
            Return M_BodyWidth
        End Get
        Set(ByVal value As Integer)
            M_BodyWidth = value
            Me.Refresh()
        End Set
    End Property
    Public Property BodyLength() As Integer
        Get
            Return M_Length
        End Get
        Set(ByVal value As Integer)
            M_Length = value
            Me.Refresh()
        End Set
    End Property

    Public Property ExitTo() As Exit_Side
        Get
            Return M_Exit_to
        End Get
        Set(ByVal value As Exit_Side)
            M_Exit_to = value
            Me.Refresh()
        End Set
    End Property
    Public Sub SetCenter(ByVal X As Integer, ByVal Y As Integer)
        P1 = New Point(X, Y)
        MoveTo()
    End Sub
    Public Sub SetCenter(ByVal Point As Point)
        P1 = New Point(Point.X, Point.Y)
        MoveTo()
    End Sub
    Public ReadOnly Property CenterPoint() As Point
        Get
            Return P1
        End Get
    End Property
    Public ReadOnly Property SocketColor() As Color
        Get
            Return M_Color
        End Get
    End Property
    Public ReadOnly Property SocketColorB() As Color
        Get
            Return M_Color_B
        End Get
    End Property

    Private Sub Plug_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If M_Exit_to = Exit_Side.To_Right Then
            M_LinePoint = New Point(P1.X + Me.Width - 5, P1.Y)
        End If
        If M_Exit_to = Exit_Side.To_Bottom Then
            M_LinePoint = New Point(P1.X, P1.Y + Me.Height - 5)
        End If
        If M_Exit_to = Exit_Side.To_Top Then
            M_LinePoint = New Point(P1.X, P1.Y - Me.Height + 5)
        End If
        If M_Exit_to = Exit_Side.To_Left Then
            M_LinePoint = New Point(P1.X - Me.Width + 5, P1.Y)
        End If
        If M_LinePoint.X <> m_oldPoint.X Or M_LinePoint.Y <> m_oldPoint.Y Then
            RaiseEvent UpdateWire(M_LinePoint)
            m_oldPoint = New Point(M_LinePoint)
        End If

    End Sub
    Private Sub button2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
        Dim BP As New Bitmap(M_Length, M_BodyWidth, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        Dim g As Graphics = Graphics.FromImage(BP)
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias

        Dim x As Integer = 0
        Dim y As Integer = (BP.Height / 2) - M_Rad
        Dim w As Integer = M_Rad * 2
        Dim h As Integer = M_Rad * 2
        'Dim TC As Color
        Dim TC As Color = SocketTypesColor.GetBodyColor(M_SocketType)

        'If M_SocketType = Socket_Type.None Then TC = Color.Gray
        'If M_SocketType = Socket_Type.Video Then TC = Color.Yellow
        'If M_SocketType = Socket_Type.Trigger Then TC = Color.DodgerBlue
        'If M_SocketType = Socket_Type.Universal Then TC = Color.White
        'If M_SocketType = Socket_Type.Image Then TC = Color.Green
        'If M_SocketType = Socket_Type.Control Then TC = Color.Orange
        'If M_SocketType = Socket_Type.CameraControl Then TC = Color.Maroon
        'If M_SocketType = Socket_Type.Selected Then TC = Color.Red
        'If M_SocketType = Socket_Type.HardwarePort Then TC = Color.DeepPink

        M_Color = TC
        Dim Br As New SolidBrush(TC)
        g.Clear(TC)
        If m_AutoBodyColor Then
            Dim Red As Integer = TC.R
            Dim Green As Integer = TC.G
            Dim Blue As Integer = TC.B
            Red = Red - 80
            If Red < 0 Then Red = 0
            Green = Green - 80
            If Green < 0 Then Green = 0
            Blue = Blue - 80
            If Blue < 0 Then Blue = 0
            m_BodyColor = Color.FromArgb(255, Red, Green, Blue)
        End If

        'Dim TCb As Color
        Dim TCb As Color = SocketTypesColor.GetIdentColor(M_SocketType)

        'If M_SocketType = Socket_Type.None Then TCb = Color.Gray
        'If M_SocketType = Socket_Type.Video Then TCb = Color.Yellow
        'If M_SocketType = Socket_Type.Trigger Then TCb = Color.DodgerBlue
        'If M_SocketType = Socket_Type.Universal Then TCb = Color.White
        'If M_SocketType = Socket_Type.Image Then TCb = Color.Black
        'If M_SocketType = Socket_Type.Control Then TCb = Color.Orange
        'If M_SocketType = Socket_Type.CameraControl Then TCb = Color.Maroon
        'If M_SocketType = Socket_Type.Selected Then TCb = Color.Red
        'If M_SocketType = Socket_Type.HardwarePort Then TCb = Color.DeepPink

        M_Color_B = TCb
        Dim BBrush As New SolidBrush(TC)
        Dim Bpen As New Pen(Color.Black)
        Dim BBrushb As New SolidBrush(TCb)
        g.FillEllipse(Br, x, y, w, h)
        g.FillEllipse(BBrushb, x, y, w, h)
        g.DrawEllipse(Bpen, x, y, w, h)
        g.FillRectangle(BBrush, 13, 0, (M_Length - M_Rad), (M_BodyWidth * 2))
        If M_Exit_to = Exit_Side.To_Left Then
            BP.RotateFlip(RotateFlipType.Rotate180FlipNone)
        End If
        If M_Exit_to = Exit_Side.To_Right Then
        End If
        If M_Exit_to = Exit_Side.To_Bottom Then
            BP.RotateFlip(RotateFlipType.Rotate90FlipNone)
        End If
        If M_Exit_to = Exit_Side.To_Top Then
            BP.RotateFlip(RotateFlipType.Rotate270FlipNone)
        End If
        w = BP.Width
        h = BP.Height
        Me.Width = BP.Width
        Me.Height = BP.Height
        e.Graphics.DrawImage(BP, 0, 0, w, h)
    End Sub
    Private Sub MoveTo()
        If P1 = Nothing Then Exit Sub
        Dim MP As Point
        If M_Exit_to = Exit_Side.To_Right Then
            MP = New Point(P1.X - M_Rad, P1.Y - (M_BodyWidth / 2))
        End If
        If M_Exit_to = Exit_Side.To_Bottom Then
            MP = New Point(P1.X - M_Rad, P1.Y - (M_BodyWidth / 2) + 1)
        End If
        If M_Exit_to = Exit_Side.To_Top Then
            MP = New Point(P1.X - (M_BodyWidth / 2), P1.Y - (M_Length - M_Rad) + 1)
        End If
        If M_Exit_to = Exit_Side.To_Left Then
            MP = New Point(P1.X - (M_Length - M_Rad) + 1, P1.Y - (M_BodyWidth / 2) + 1)
        End If
        Me.Location = MP
        Me.Refresh()
    End Sub
    Public Sub PointTo(ByVal Socket As Socket)
        P1 = New Point(Socket.CenterPoint.X, Socket.CenterPoint.Y)
        M_Exit_to = Socket.ExitTo
        MoveTo()
        If M_Exit_to = Exit_Side.To_Right Then
            M_LinePoint = New Point(P1.X + Me.Width - 5, P1.Y)
        End If
        If M_Exit_to = Exit_Side.To_Bottom Then
            M_LinePoint = New Point(P1.X, P1.Y + Me.Height - 5)
        End If
        If M_Exit_to = Exit_Side.To_Top Then
            M_LinePoint = New Point(P1.X, P1.Y - Me.Height + 5)
        End If
        If M_Exit_to = Exit_Side.To_Left Then
            M_LinePoint = New Point(P1.X - Me.Width + 5, P1.Y)
        End If
        m_oldPoint = New Point(M_LinePoint.X, M_LinePoint.Y)
        RaiseEvent UpdateWire(M_LinePoint)
        Me.Refresh()
    End Sub
    Public Sub New()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.BackColor = Color.Transparent
        Me.AllowDrop = True
    End Sub
    Public Property AutoBodyColor() As Boolean
        Get
            Return m_AutoBodyColor
        End Get
        Set(ByVal value As Boolean)
            m_AutoBodyColor = value
            Me.Refresh()
        End Set
    End Property
    Public Property Radius() As Integer
        Get
            Return M_Rad
        End Get
        Set(ByVal value As Integer)
            M_Rad = value
            Me.Refresh()
        End Set
    End Property
    Public Property SocketType() As Socket_Type
        Get
            Return M_SocketType
        End Get
        Set(ByVal value As Socket_Type)
            M_SocketType = value
            Me.Refresh()
        End Set
    End Property
    Public Function GetLinePoint() As Point
        If M_Exit_to = Exit_Side.To_Right Then
            M_LinePoint = New Point(P1.X + Me.Width - 5, P1.Y)
        End If
        If M_Exit_to = Exit_Side.To_Bottom Then
            M_LinePoint = New Point(P1.X, P1.Y + Me.Height - 5)
        End If
        If M_Exit_to = Exit_Side.To_Top Then
            M_LinePoint = New Point(P1.X, P1.Y - Me.Height + 5)
        End If
        If M_Exit_to = Exit_Side.To_Left Then
            M_LinePoint = New Point(P1.X - Me.Width + 5, P1.Y)
        End If
        Return M_LinePoint
    End Function
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
        Me.DisconnectToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.Button1 = New System.Windows.Forms.Button
        Me.ContextMenuStrip1.SuspendLayout()
        Me.SuspendLayout()
        '
        'ContextMenuStrip1
        '
        Me.ContextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.DisconnectToolStripMenuItem})
        Me.ContextMenuStrip1.Name = "ContextMenuStrip1"
        Me.ContextMenuStrip1.Size = New System.Drawing.Size(138, 26)
        '
        'DisconnectToolStripMenuItem
        '
        Me.DisconnectToolStripMenuItem.Name = "DisconnectToolStripMenuItem"
        Me.DisconnectToolStripMenuItem.Size = New System.Drawing.Size(137, 22)
        Me.DisconnectToolStripMenuItem.Text = "Disconnect"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(5, 5)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(15, 15)
        Me.Button1.TabIndex = 1
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Plug
        '
        Me.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.ContextMenuStrip = Me.ContextMenuStrip1
        Me.Controls.Add(Me.Button1)
        Me.DoubleBuffered = True
        Me.Name = "Plug"
        Me.Size = New System.Drawing.Size(148, 148)
        Me.ContextMenuStrip1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

    Private Sub DisconnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectToolStripMenuItem.Click
        RaiseEvent Disconnect()
    End Sub
End Class
